package lee.vioson.ebook.ui.components

import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laiyifen.lyftoast.isNotEmpty
import lee.vioson.composecommon.R
import lee.vioson.composecommon.widgets.Input
import lee.vioson.composecommon.widgets.placeHolder
import lee.vioson.ebook.ui.AppTheme

/**
 *Author:viosonlee
 *Date:2023/6/8
 *DESCRIPTION:
 */
@Composable
fun InputFiled(
    modifier: Modifier,
    value: String,
    onValueChange: (String) -> Unit,
    @StringRes placeHolder: Int,
    error: String = "",
    leftIcon: (@Composable () -> Unit)? = null,
    isPassword: Boolean = false,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default
) {
    var pwdVisible by remember { mutableStateOf(false) }
    Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
        Input(
            inputTxt = value,
            onValueChange = onValueChange,
            modifier = Modifier
                .height(54.dp)
                .fillMaxWidth()
                .border(
                    width = 1.5.dp,
                    color = if (error.isNotEmpty) AppTheme.colors.error else
                        AppTheme.colors.onBackground,
                    shape = RoundedCornerShape(16.dp)
                )
                .clip(RoundedCornerShape(16.dp))
                .background(AppTheme.colors.onBackground),
            textStyle = TextStyle(
                fontSize = 18.sp,
                color = AppTheme.appColors.inputColor
            ),
            placeholder = placeHolder(
                id = placeHolder,
                textStyle = TextStyle(
                    fontSize = 18.sp,
                    color = AppTheme.appColors.hintColor
                )
            ),
            trailingIcon = {
                if (isPassword) {
                    Box(modifier = Modifier.padding(horizontal = 12.dp)) {
                        TogglePwdVisible(
                            pwdVisible = pwdVisible,
                            onVisibleChange = { pwdVisible = it })
                    }
                }
            },
            leadingIcon = {
                if (leftIcon != null) {
                    Box(modifier = Modifier.padding(horizontal = 12.dp)) {
                        leftIcon()
                    }
                }
            },
            visualTransformation = if (isPassword && !pwdVisible) PasswordVisualTransformation() else VisualTransformation.None,
            keyboardOptions = keyboardOptions,
        )
        if (error.isNotEmpty) {
            Text(text = error, style = TextStyle(fontSize = 16.sp, color = AppTheme.colors.error))
        }
    }
}

@Composable
fun TogglePwdVisible(pwdVisible: Boolean, onVisibleChange: (Boolean) -> Unit) {
    Icon(
        painter = painterResource(id = if (pwdVisible) R.drawable.icon_pwd_open else R.drawable.icon_pwd_close),
        contentDescription = null,
        modifier = Modifier
            .size(28.dp)
            .clickable {
                onVisibleChange(!pwdVisible)
            },
        tint = AppTheme.appColors.inputColor
    )
}